//
// Created by shen on 2025/3/22.
//

#ifndef VIDEOEDIT_FAVFRAMEHELPER_H
#define VIDEOEDIT_FAVFRAMEHELPER_H
/* SWAP 宏交换两个变量的值。*/

#include <chrono>   // 时间库
#include <thread>   // 线程库
#include "iostream"
#include "SWorkStatus.h"
#include "JavaCallHelper.h"
#include "SafeQueue.h"
#include "LogUtil.h"
#include "mutex"
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/time.h"
#include "libavutil/frame.h"
};
class FavFrameHelper {

    static void t_decode_avPack(FavFrameHelper *mFavFrameHelper);
    static void *startPutAvPacket(FavFrameHelper *mFavFrameHelper);
    static void *startGetFrame(FavFrameHelper *mFavFrameHelper);
public:
    FavFrameHelper(JavaCallHelper *_helper,const char *_url,FFStatus *kzgPlayerStatus);
    virtual ~FavFrameHelper();

public:
    std::thread *thread_start_get_frame;
    std::thread *thread_start_decode_frame;

    JavaCallHelper *helper;
     char url[1024];
    FFStatus *playerStatus;

    SafeQueue *queue;

    AVFormatContext *avFormatContext;

    std::thread *decodeAvPacketThread;
    std::mutex mtx;
    std::mutex codecMutex;
    std::mutex init_mutex;

    bool isExit = false;
    bool isPause = true;

    int avStreamIndex = -1;
    AVRational time_base;
    AVCodecContext *avCodecContext = NULL;
    int64_t duration = 0;
public:
    void init();
    int getAvCodecContent(AVCodecParameters *avCodecParameters,AVCodecContext **avCodecContext);

    void decodeAVPackate();

    void startAvFrame();

    void decodeFrameFromQueue();

    void decodeFrame();
};


#endif //VIDEOEDIT_FAVFRAMEHELPER_H
